package com.atguigu.spzx.manage.service.Impl;


import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.manage.mapper.EventRegisterMapper;
import com.atguigu.spzx.manage.service.EventRegisterService;
import com.atguigu.spzx.model.dto.system.EventRegisterDto;
import com.atguigu.spzx.model.entity.system.EventRegister;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class EventRegisterServiceImpl implements EventRegisterService {

    @Autowired
    private EventRegisterMapper eventRegisterMapper;
    @Override
    public PageInfo<EventRegister> findByPage(EventRegisterDto eventRegisterDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum , pageSize);
        List<EventRegister> eventRegisterList = eventRegisterMapper.findByPage(eventRegisterDto) ;
        PageInfo pageInfo = new PageInfo(eventRegisterList) ;
        return pageInfo;
    }

    @Override
    public void saveRegister(EventRegister eventRegister) {
        String eventName= eventRegister.getEventName();
        String username=eventRegister.getUserName();
        //查看该报名是否在数据库
        EventRegister dbEventRegister=eventRegisterMapper.selectByUserName(username);
        //如果活动名和用户名已存在,则新增的报名不能加入数据库

        if(dbEventRegister!=null &&((username.equals(dbEventRegister.getUserName()) && eventName.equals(dbEventRegister.getEventName())))){
                throw new GuiguException(ResultCodeEnum.USER_REGISTER_IS_EXISTS);
            }
        //报名状态
        eventRegister.setStatus(0);


        eventRegisterMapper.saveRegister(eventRegister);







    }

    @Override
    public void updateRegister(EventRegister eventRegister) {

        eventRegisterMapper.updateById(eventRegister);

    }


//审核
/*    @Override
    public void auditRegister(Long id, Integer status) {
        //查询该id
        EventRegister eventRegister=eventRegisterMapper.selectById(id);
        eventRegister.setStatus(status);
        eventRegisterMapper.updateById(eventRegister);

    }*/
//通过
    @Override
    public void passById(Long id) {
        EventRegister eventRegister=eventRegisterMapper.selectById(id);
        eventRegister.setStatus(1);
        eventRegisterMapper.updateById(eventRegister);
    }
//驳回
    @Override
    public void rejectById(Long id) {
        EventRegister eventRegister=eventRegisterMapper.selectById(id);
        eventRegister.setStatus(2);
        eventRegisterMapper.updateById(eventRegister);
    }

    @Override
    public void deleteById(Long id) {
        eventRegisterMapper.deleteById(id);

    }
}
